const axios = require('axios')

const HOST_URL = ''

const request = axios.create({
  baseURL: HOST_URL,
  timeout: 10000,
  // 添加以下配置来避免 ReadableStream 问题
  responseType: 'json',
  // 禁用默认的响应转换
  transformResponse: [(data) => data],
})

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    return response
  },
  (error) => {
    return Promise.reject(error)
  }
)
// 请求拦截器
request.interceptors.request.use(
  (config) => {
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)
const post = async (url, data, headers) => {
  try {
    return request({
      url,
      method: 'post',
      data,
      headers,
      maxRedirects: 0,
    })
  } catch (err) {
    console.log(err, '<< err')
  }
}

const get = async (url, params, headers) => {
  try {
    return request({
      url,
      method: 'get',
      params,
      headers,
      maxRedirects: 0,
    })
  } catch (err) {
    console.log(err, '<< err')
  }
}

module.exports = {
  get,
  post,
}
